#include <curl/curl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>

using std::string;

static size_t writetobuffer(void *ptr, size_t size, size_t nmemb, void *data)
{
    size_t realsize = size * nmemb;
    string *userdata = (string *)data;

    if (userdata != NULL)
    {
        userdata->append((char *)ptr, realsize);
    }
    else
        fprintf(stderr, "buffer is empty\n");
    return realsize;
}

int main(int argc, char **argv)
{
    CURL *curl;
    CURLcode res;

    struct curl_httppost *formpost=NULL;
    struct curl_httppost *lastptr=NULL;
    struct curl_slist *headerlist=NULL;
    static const char buf[] = "Expect:";
    string resp_buffer;

    curl_global_init(CURL_GLOBAL_ALL);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "Email",
                 CURLFORM_COPYCONTENTS, "birdmanyan@gmail.com",
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "Passwd",
                 CURLFORM_COPYCONTENTS, "icbm1987",
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "source",
                 CURLFORM_COPYCONTENTS, "bird-googlesync-1",
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_COPYNAME, "service",
                 CURLFORM_COPYCONTENTS, "cl",
                 CURLFORM_END);

    curl_formadd(&formpost,
                 &lastptr,
                 CURLFORM_CONTENTTYPE, "application/x-www-form-urlencoded",
                 CURLFORM_END);

    curl = curl_easy_init();

    headerlist = curl_slist_append(headerlist, buf);

    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
        
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writetobuffer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp_buffer);

        
        res = curl_easy_perform(curl);


        string auth_note = "Auth";
        string auth = resp_buffer.substr(resp_buffer.find(auth_note)+auth_note.size());
        auth = auth.substr(0, auth.size() - 1);

        auth = "Authorization: GoogleLogin auth"+auth;

        resp_buffer.clear();

        curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/calendar/feeds/default/allcalendars/full");

        curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
        struct curl_slist *headerlist2 = NULL;

        headerlist2 = curl_slist_append(headerlist2, "alt=jsonc");

//        headerlist2 = curl_slist_append(headerlist2, auth.c_str());
        
//        headerlist2 = curl_slist_append(headerlist2, "Content-Type:application/json");

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist2);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);


        res = curl_easy_perform(curl);

        printf("the content is %s\n", resp_buffer.c_str());

        curl_easy_cleanup(curl);

        curl_slist_free_all(headerlist);
        curl_slist_free_all(headerlist2);
    }

    return 0;
}

